<template>
	<view>

		<view style="font-size: 35rpx; font-weight: 600; height: 70rpx; margin: 15rpx auto; line-height: 70rpx;  width: 80%;">
			<picker @change="bindPickerChange" :value="index" :range="myYears">
				<view class="uni-input">
					当前显示：
					<text style="color: #02C9FC;">{{myYears[index]}}</text>
					年
				</view>
			</picker>
		</view>

		<view class="qiun-columns">
			<!--#ifdef H5 -->

			<!--#endif-->

			<view class="qiun-charts">
				<!--#ifdef MP-ALIPAY -->
				<canvas canvas-id="canvasLineA" id="canvasLineA" class="charts" :width="cWidth*pixelRatio" :height="cHeight*pixelRatio"
				 :style="{'width':cWidth+'px','height':cHeight+'px'}" @touchstart="touchLineA"></canvas>
				<!--#endif-->
				<!--#ifndef MP-ALIPAY -->
				<canvas canvas-id="canvasLineA" id="canvasLineA" class="charts" @touchstart="touchLineA"></canvas>
				<!--#endif-->
			</view>
			<!--#ifdef H5 -->
			<!--#endif-->
		</view>

		<view>
			<view class="title">
				<view style="flex: 5; text-align: center;">补贴月份</view>
				<view style="flex: 5; text-align: center;">补贴金额</view>
				<view style="flex: 5; text-align: center;">增补金额</view>
			</view>
			<view v-if="listData.length > 0" >
				<view v-for="(item,iindex) in listData" :key="iindex" class="list">
					<view style="flex: 5; text-align: center;">{{myYears[index]}}-{{item.mon}}</view>
					<view style="flex: 5; text-align: center;">{{item.subsidyMoney}}</view>
					<view style="flex: 5; text-align: center;">{{item.addMoney}}</view>
				</view>
			</view>
			<view v-else>
				<view style="margin: 50rpx auto; text-align: center; font-size: 50rpx; font-weight: 800; color: #02C9FC;" >
					当年暂无补贴记录！
				</view>
			</view>
		</view>
	</view>

</template>

<script>
	import uCharts from '@/js_sdk/u-charts/u-charts/u-charts.js';
	import {
		isJSON
	} from '@/common/checker.js';

	var _self;
	var canvaLineA = null;
	export default {
		data() {
			// var myYears=[];
			// const date = new Date().getFullYear()
			// for(var i = 1970;i++;i<=date){
			// 	myYears.push(i) 
			// }
			return {
				cWidth: '',
				cHeight: '',
				pixelRatio: 1,
				textarea: '',
				myYears:[],
				index: 0,
				listData:[]
			}
		},
		onLoad() {
			
			var myYears=[];
			var date = new Date().getFullYear()
			for(var i = 1970;i<=date;i++){
				myYears.push(i) 
			}
			this.myYears = myYears
			this.index = myYears.length-1
			console.log(myYears)
			
			_self = this;
			//#ifdef MP-ALIPAY
			uni.getSystemInfo({
				success: function(res) {
					if (res.pixelRatio > 1) {
						//正常这里给2就行，如果pixelRatio=3性能会降低一点
						//_self.pixelRatio =res.pixelRatio;
						_self.pixelRatio = 2;
					}
				}
			});
			//#endif
			this.cWidth = uni.upx2px(750);
			this.cHeight = uni.upx2px(500);
			this.getServerData();
		},
		methods: {
			bindPickerChange(e) {
				this.index = e.detail.value
				this.getServerData(this.myYears[e.detail.value])
			},
			toTime(timestamp) {
				//获取当前时间
				var now = new Date();
				//根据指定时间戳转换为时间格式
				var time = new Date();
				time.setTime(timestamp);
				//比较当前时间和指定时间的差来决定显示时间格式
				//1.年份与当前不同则显示完整日期 yyyy-MM-dd hh:mm
				if (time.getFullYear() != now.getFullYear())
					return time.getFullYear() + "-" +
						((time.getMonth() + 1) < 10 ? "0" + (time.getMonth() + 1) : (time.getMonth() + 1)) + "-" +
						(time.getDate() < 10 ? "0" + time.getDate() : time.getDate()) + " " +
						(time.getHours() < 10 ? "0" + time.getHours() : time.getHours()) + ":" +
						(time.getMinutes() < 10 ? "0" + time.getMinutes() : time.getMinutes());
				//2.年份与当前相同但月份或日期不同时 显示 MM-dd hh:mm格式
				else if (time.getMonth() != now.getMonth() || time.getDate() != now.getDate())
					return ((time.getMonth() + 1) < 10 ? "0" + (time.getMonth() + 1) : (time.getMonth() + 1)) + "-" +
						(time.getDate() < 10 ? "0" + time.getDate() : time.getDate()) + " " +
						(time.getHours() < 10 ? "0" + time.getHours() : time.getHours()) + ":" +
						(time.getMinutes() < 10 ? "0" + time.getMinutes() : time.getMinutes());
				//3.年份与日期均与当前相同时，显示hh:mm格式
				else
					return (time.getHours() < 10 ? "0" + time.getHours() : time.getHours()) + ":" +
						(time.getMinutes() < 10 ? "0" + time.getMinutes() : time.getMinutes());
			},
			async getServerData(year=0) {
				var that = this;
				if(year == 0){
					year = new Date().getFullYear();
				}
				console.log(year)
				var res = []
				var data = [0,0,0,0,0,0,0,0,0,0,0,0]
				try{
					res = await that.$api.getPersonHistory({
						subsidyYear:2020
					})
					res = res.data
					
				}catch(e){
					//TODO handle the exception
					res = []
				}
				
				console.log(res)
				that.listData = []
				var s
				res.forEach(e=>{
					
					s = this.toTime(e.subsidyDate)
					var mon = s.substr(0,2);
					
					that.listData.push({
						mon,
						addMoney:e.addMoney,
						subsidyMoney:e.subsidyMoney
					})
					mon = parseInt(mon)
					data[mon-1] = e.addMoney + e.subsidyMoney
					console.log(`mon = ${mon} , data = ${data[mon]}`)
					
					
				})
				
				let LineA = {
					categories: [],
					series: []
				};
				LineA.categories = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'];
				LineA.series = [{
					"name": "补贴金额",
					"data":data
					// "data": [35, 8, 25, 37, 4, 20, 600, 700, 700, 500, 600, 900],
				}]

				_self.showLineA("canvasLineA", LineA);


			},
			showLineA(canvasId, chartData) {
				canvaLineA = new uCharts({
					$this: _self,
					canvasId: canvasId,
					type: 'line',
					fontSize: 11,
					padding: [15, 20, 0, 15],
					legend: {
						show: true,
						padding: 5,
						lineHeight: 11,
						margin: 0,
					},
					dataLabel: true,
					dataPointShape: true,
					background: '#FFFFFF',
					pixelRatio: _self.pixelRatio,
					categories: chartData.categories,
					series: chartData.series,
					animation: true,
					xAxis: {
						type: 'grid',
						gridColor: '#CCCCCC',
						gridType: 'dash',
						dashLength: 8,
						boundaryGap: 'justify'
					},
					yAxis: {
						gridType: 'dash',
						gridColor: '#CCCCCC',
						dashLength: 8,
						splitNumber: 5,
						format: (val) => {
							return val.toFixed(0) + '元'
						}
					},
					width: _self.cWidth * _self.pixelRatio,
					height: _self.cHeight * _self.pixelRatio,
					extra: {
						line: {
							type: 'curve'
						}
					}
				});
				//下面是默认选中索引
				let cindex = 3;
				//下面是自定义文案
				// let textList=[{text:'我是一个标题',color:null},{text:'自定义1：值1',color:'#2fc25b'},{text:'自定义2：值2',color:'#facc14'},{text:'自定义3：值3',color:'#f04864'}];
				//下面是event的模拟,tooltip的Y坐标值通过这个mp.changedTouches[0].y控制
				let tmpevent = {
					mp: {
						changedTouches: [{
							x: 0,
							y: 80
						}]
					}
				};
				setTimeout(() => {
					canvaLineA.showToolTip(tmpevent, {
						index: cindex
						// textList:textList
					});
				}, 200)
			},
			touchLineA(e) {
				canvaLineA.touchLegend(e);
				canvaLineA.showToolTip(e, {
					format: function(item, category) {
						return category + ' ' + item.name + ':' + item.data
					}
				});
			},
			changeData() {
				if (isJSON(_self.textarea)) {
					let newdata = JSON.parse(_self.textarea);
					canvaLineA.updateData({
						series: newdata.series,
						categories: newdata.categories
					});
				} else {
					uni.showToast({
						title: '数据格式错误',
						image: '../../../static/images/alert-warning.png'
					})
				}
			}
		}
	}
</script>

<style>
	.list {
		border-bottom: 1px solid #CCCCCC;
		width: 100%;
		display: flex;
		height: 100rpx;
		line-height: 100rpx;
		font-size: 30rpx;
	}

	.title {
		/* 		border-bottom: 1px solid #CCCCCC; */
		margin-top: 30rpx;
		width: 100%;
		display: flex;
		height: 100rpx;
		line-height: 100rpx;
		font-weight: 600;
		font-size: 30rpx;
		background-color: #F1F1F1;
	}

	/*样式的width和height一定要与定义的cWidth和cHeight相对应*/
	.qiun-charts {
		width: 750upx;
		height: 500upx;
		background-color: #FFFFFF;
	}

	.charts {
		width: 750upx;
		height: 500upx;
		background-color: #FFFFFF;
	}
</style>
